/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.data;

import aptech.entities.Users;
import java.util.List;
import org.hibernate.Query;

/**
 *
 * @author home
 */
public class UserBO extends ManagerBase<Users> {

    public UserBO() throws Exception {
    }

    public Users login(String username, String password) throws Exception {
        beginTransaction();
        String sql = "from Users where usernameLogin =:username and passwordLogin=:password";
        Query query = getCurrentSession().createQuery(sql);
        query.setParameter("username", username);
        query.setParameter("password", password);
        List<Users> list = query.list();
        if(list.size() != 0){
            return list.get(0);
        }
        return null;
    }

    public boolean checkUsernameExist(String username) throws Exception {
        boolean status = false;
        beginTransaction();
        String sql = "from Users where usernameLogin =:username";
        Query query = getCurrentSession().createQuery(sql);
        query.setParameter("username", username);
        List<Users> list = query.list();
        if (list.size() != 0) {
            status = true;
        }
        commitAndClose();
        return status;
    }

    public boolean checkEmailExist(String email) throws Exception {
        boolean status = false;
        beginTransaction();
        String sql = "from Users where emailLogin =:email";
        Query query = getCurrentSession().createQuery(sql);
        query.setParameter("email", email);
        List<Users> list = query.list();
        if (list.size() != 0) {
            status = true;
        }
        commitAndClose();
        return status;
    }

    public boolean checkActive(String username) throws Exception {
        boolean status = false;
        beginTransaction();
        String sql = "from Users where usernameLogin =:username and statusActive=:st";
        Query query = getCurrentSession().createQuery(sql);
        query.setParameter("username", username);
        query.setParameter("st", "False");
        List<Users> list = query.list();
        if (list.size() != 0) {
            status = true;
        }
        commitAndClose();
        return status;
    }

    public boolean activeAccount(String keycode) throws Exception{
        boolean status = false;
        beginTransaction();
        Query query = getCurrentSession().createQuery("update Users set statusActive='True' where codeActive=:keycode");
        query.setParameter("keycode", keycode);
        int ex = query.executeUpdate();
        if(ex > 0){
            status = true;
        }
        commitAndClose();
        return status;
    }
}
